<script setup>
import {getCurrentInstance, reactive, ref} from "vue";
import {cloneDeep} from "lodash";
import {addSysNotice, getSysNotice, updateSysNotice} from "@/api/system/notice";
import {noticeStatusList, noticeTypeList} from "@/enum/enum";
import {enumToArray} from "@/utils/utils";

const {proxy} = getCurrentInstance();
const emit = defineEmits(['success']);
const modalRef = ref();
const formRef = ref();
const formData = ref({
  noticeId: undefined,
  noticeTitle: '',
  noticeType: 1,
  noticeContent: '',
  status: 1,
  remark: '',
})
const rules = reactive({
  noticeTitle: [{required: true, message: '请输入公告标题', trigger: 'blur'}],
  noticeContent: [{required: true, message: '请输入公告内容', trigger: 'blur'}]
})
const noticeTypeOptions = enumToArray(noticeTypeList);
const noticeStatusOptions = enumToArray(noticeStatusList);
const formType = ref('create')


const open = async (noticeId, type = 'create') => {
  formType.value = type;
  resetForm();
  if (type === 'create') {
  } else {
    const {data} = await getSysNotice(noticeId);
    formData.value = {
      ...data
    }
  }
  modalRef.value.handleOpen()
}

const handleSubmit = async () => {
  await formRef.value.validate();
  let data = cloneDeep(formData.value);
  if (data.noticeId === undefined) {
    await addSysNotice(data);
    proxy.$modal.okNotify('新增成功');
  } else {
    await updateSysNotice(data);
    proxy.$modal.okNotify('修改成功');
  }
  resetForm();
  modalRef.value.handleClose();
  emit('success')
}

const resetForm = () => {
  formRef.value?.resetFields();
  formData.value = {
    noticeId: undefined,
    noticeTitle: '',
    noticeType: 1,
    noticeContent: '',
    status: 1,
    remark: '',
  }
}

defineExpose({open})
</script>

<template>
  <dialog-form ref="modalRef" :modalTitle="`${formType === 'create' ? '新建' : '编辑'}参数`" modalWidth="1200px"
               @submit="handleSubmit">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
      <el-form-item label="公告标题" prop="noticeTitle">
        <el-input v-model="formData.noticeTitle" placeholder="请输入公告标题"/>
      </el-form-item>
      <el-form-item label="公告类型" prop="noticeType">
        <el-select v-model="formData.noticeType" placeholder="请选择公告类型">
          <el-option v-for="(item, index) in noticeTypeOptions" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公告状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择公告状态">
          <el-option v-for="(item, index) in noticeStatusOptions" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公告内容" prop="noticeContent">
        <editor v-model="formData.noticeContent" placeholder="请输入公告内容"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="formData.remark" placeholder="请输入内容"/>
      </el-form-item>
    </el-form>
  </dialog-form>
</template>

<style scoped lang="scss">

</style>
